'use strict';

// const Controller = require('egg').Controller;

const baseController = require('../../utils/base')

class ManagerController extends baseController {
    async index() {

        let result = await this.app.mysql.query('select admin.id,username,mobile,email,r.title from admin left join role r on admin.role_id = r.id')
        await this.ctx.render(`admin/manager/index`, {
                list: result
            })
            // this.ctx.body = "管理员 列表"
    }
    async add() {

        let roleList = await this.app.mysql.query('select * from role')
        console.log(roleList[0].title, "----------------");

        await this.ctx.render(`admin/manager/add`, {
            roleList: roleList
        })
    }
    async doAdd() {

        let { username, password, mobile, email, roleId, is_super, status } = this.ctx.request.body
        if (username == "") {
            return await this.error('用户名不能为空', `/${this.config.pathname}/manager/add`)
        }
        if (password == "") {
            return await this.error('密码不能为空', `/${this.config.pathname}/manager/add`)
        }

        let result = await this.app.mysql.query('select * from admin where username = ?', [username])
        console.log(result, '----');
        if (result.length > 0) {
            return await this.error('用户名已存在', `/${this.config.pathname}/manager/add`)
        } else {
            console.log('-+-+-+-+-+-+-+-+-+');
            const results = await this.app.mysql.query('insert into admin (username, password, mobile, email, status, role_id, add_time, is_super) VALUES (?,?,?,?,?,?,?,?)', [username, await this.service.admin.utlis.md5(password), mobile, email, status, roleId, await this.service.admin.utlis.getDate(), is_super])
            return await this.success('添加成功', `/${this.config.pathname}/manager`)
        }

    }
    async edit() {
        let { id } = this.ctx.query
        let result = await this.app.mysql.query('select * from admin where id = ?', [id])
        let roleList = await this.app.mysql.query('select * from role')

        console.log(result[0], roleList, "---");
        await this.ctx.render('admin/manager/edit', {
            list: result[0],
            roleList
        })
    }
    async doEdit() {
        let { username, password, mobile, email, status, roleId, is_super, id } = this.ctx.request.body

        let result = await this.app.mysql.query("update admin set username = ?,password=?,mobile=?,email=?,status=?,role_id=?,is_super=? where id = ?", [username, password, mobile, email, status, roleId, is_super, id])
        console.log(result, "=-=-=-=-=-=");
        if (result) {
            await this.success('更新成功', `/${this.config.pathname}/manager`)
        } else {
            await this.success('更新失败', `/${this.config.pathname}/manager`)
        }
    }
    async del() {
        try {
            let { id } = this.ctx.query
            const result = await this.app.mysql.query('delete from admin where id = ?', [id])
            if (result) {
                await this.success('删除成功', `/${this.config.pathname}/manager`)
            } else {
                await this.success('删除失败', `/${this.config.pathname}/manager`)
            }
        } catch (error) {
            await this.success(error + '异常报错', `/${this.config.pathname}/manager`)
        }

    }
}

module.exports = ManagerController;